<template>
  <div class="app-container" align="center" style="width: 1300px">
    <el-card class="box-card" align="center">
      <div slot="header" class="clearfix">
        <span>密码修改</span>
      </div>
      <div>
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-row type="flex" justify="center" align="top">
            <el-col :span="15">
              <el-form-item label="密码" prop="password">
                <el-input v-model="formData.password" type="password" placeholder="请输入密码" :maxlength="20" show-word-limit
                          clearable :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="center" align="top">
            <el-col :span="15">
              <el-form-item label="新密码" prop="newPassword">
                <el-input v-model="formData.newPassword" type="password" placeholder="请输入新密码" :maxlength="20" show-word-limit
                          clearable :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="center" align="top">
            <el-col :span="15">
              <el-form-item label="确认新密码" prop="checkPass">
                <el-input v-model="formData.checkPass" type="password" placeholder="请再次输入新密码" :maxlength="20" show-word-limit
                          clearable :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" justify="center" align="top">
            <el-col :span="15">
              <el-form-item size="large">
                <el-button type="primary" @click="submitForm"> 提交</el-button>
                <el-button @click="resetForm">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
  </div>

</template>
<script>
  import {mapGetters} from "vuex";
  import {getInfo} from '@/api/user'
  import request from "@/utils/request";

  export default {
    computed: {
      ...mapGetters([
        'name',
        'id',
        'user'
      ])
    },
    components: {},
    props: [],

    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      var validateNewPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          callback();
        }
      };
      var validateNewPass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'));
        } else if (value !== this.formData.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        formData: {
          password: '',
          newPassword: '',
          checkPass: '',
          submit: '',
          reset: '',
        },
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          newPassword: [
            { validator: validateNewPass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validateNewPass2, trigger: 'blur' }
          ],
        },
      }
    },
    watch: {},
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm() {
        this.$refs['elForm'].validate(valid => {
          if (valid) {
            if (this.formData.password !== this.user.password) {
              return this.$message.error('密码输入错误');
            } else {
              const params = this.user
              params.password = this.formData.newPassword
              request({
                url: '/user/updateUser',
                method: 'get',
                params
              }).then(response => {
                console.log("发送请求")
                console.log(response.retCode)
                if (response.retCode !== '200') return this.$message.error(response.retMsg)
                this.$message.success('发送请求成功！')
                this.$refs['elForm'].resetFields()
              })
            }

          }
        })
      },
      resetForm() {
        this.$refs['elForm'].resetFields()
      },

    }
  }

</script>
<style>

  .box-card {
    width: 1000px;
    margin-left: 80px;
  }
</style>
